1
|
|
|
/* global kirkiL10n */ |
2
|
|
|
var kirki = kirki || {}; |
|
|
|
|
3
|
|
|
kirki = jQuery.extend( kirki, { |
4
|
|
|
/** |
5
|
|
|
* An object containing definitions for input fields. |
6
|
|
|
* |
7
|
|
|
* @since 3.0.16 |
8
|
|
|
*/ |
9
|
|
|
input: { |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Radio input fields. |
13
|
|
|
* |
14
|
|
|
* @since 3.0.17 |
15
|
|
|
*/ |
16
|
|
|
radio: { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Init the control. |
20
|
|
|
* |
21
|
|
|
* @since 3.0.17 |
22
|
|
|
* @param {Object} control - The control object. |
23
|
|
|
* @param {Object} control.id - The setting. |
24
|
|
|
* @returns {null} |
25
|
|
|
*/ |
26
|
|
|
init: function( control ) { |
27
|
|
|
var input = jQuery( 'input[data-id="' + control.id + '"]' ); |
|
|
|
|
28
|
|
|
|
29
|
|
|
// Save the value |
30
|
|
|
input.on( 'change keyup paste click', function() { |
31
|
|
|
kirki.setting.set( control.id, jQuery( this ).val() ); |
32
|
|
|
}); |
33
|
|
|
} |
34
|
|
|
}, |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Color input fields. |
38
|
|
|
* |
39
|
|
|
* @since 3.0.16 |
40
|
|
|
*/ |
41
|
|
|
color: { |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Init the control. |
45
|
|
|
* |
46
|
|
|
* @since 3.0.16 |
47
|
|
|
* @param {Object} control - The control object. |
48
|
|
|
* @param {Object} control.id - The setting. |
49
|
|
|
* @param {Object} control.choices - Additional options for the colorpickers. |
50
|
|
|
* @param {Object} control.params - Control parameters. |
51
|
|
|
* @param {Object} control.params.choices - alias for control.choices. |
52
|
|
|
|
53
|
|
|
* @returns {null} |
54
|
|
|
*/ |
55
|
|
|
init: function( control ) { |
56
|
|
|
var picker = jQuery( '.kirki-color-control[data-id="' + control.id + '"]' ), |
|
|
|
|
57
|
|
|
clear; |
58
|
|
|
|
59
|
|
|
control.choices = control.choices || {}; |
60
|
|
|
if ( _.isEmpty( control.choices ) && control.params.choices ) { |
61
|
|
|
control.choices = control.params.choices; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// If we have defined any extra choices, make sure they are passed-on to Iris. |
65
|
|
|
if ( ! _.isEmpty( control.choices ) ) { |
66
|
|
|
picker.wpColorPicker( control.choices ); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Tweaks to make the "clear" buttons work. |
70
|
|
|
setTimeout( function() { |
71
|
|
|
clear = jQuery( '.kirki-input-container[data-id="' + control.id + '"] .wp-picker-clear' ); |
72
|
|
|
if ( clear.length ) { |
73
|
|
|
clear.click( function() { |
74
|
|
|
kirki.setting.set( control.id, '' ); |
75
|
|
|
}); |
76
|
|
|
} |
77
|
|
|
}, 200 ); |
78
|
|
|
|
79
|
|
|
// Saves our settings to the WP API |
80
|
|
|
picker.wpColorPicker({ |
81
|
|
|
change: function() { |
82
|
|
|
|
83
|
|
|
// Small hack: the picker needs a small delay |
84
|
|
|
setTimeout( function() { |
85
|
|
|
kirki.setting.set( control.id, picker.val() ); |
86
|
|
|
}, 20 ); |
87
|
|
|
} |
88
|
|
|
}); |
89
|
|
|
} |
90
|
|
|
}, |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Generic input fields. |
94
|
|
|
* |
95
|
|
|
* @since 3.0.17 |
96
|
|
|
*/ |
97
|
|
|
genericInput: { |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Init the control. |
101
|
|
|
* |
102
|
|
|
* @since 3.0.17 |
103
|
|
|
* @param {Object} control - The control object. |
104
|
|
|
* @param {Object} control.id - The setting. |
105
|
|
|
* @returns {null} |
106
|
|
|
*/ |
107
|
|
|
init: function( control ) { |
108
|
|
|
var input = jQuery( 'input[data-id="' + control.id + '"]' ); |
|
|
|
|
109
|
|
|
|
110
|
|
|
// Save the value |
111
|
|
|
input.on( 'change keyup paste click', function() { |
112
|
|
|
kirki.setting.set( control.id, jQuery( this ).val() ); |
113
|
|
|
}); |
114
|
|
|
} |
115
|
|
|
}, |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Generic input fields. |
119
|
|
|
* |
120
|
|
|
* @since 3.0.17 |
121
|
|
|
*/ |
122
|
|
|
textarea: { |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Init the control. |
126
|
|
|
* |
127
|
|
|
* @since 3.0.17 |
128
|
|
|
* @param {Object} control - The control object. |
129
|
|
|
* @param {Object} control.id - The setting. |
130
|
|
|
* @returns {null} |
131
|
|
|
*/ |
132
|
|
|
init: function( control ) { |
133
|
|
|
var textarea = jQuery( 'textarea[data-id="' + control.id + '"]' ); |
|
|
|
|
134
|
|
|
|
135
|
|
|
// Save the value |
136
|
|
|
textarea.on( 'change keyup paste click', function() { |
137
|
|
|
kirki.setting.set( control.id, jQuery( this ).val() ); |
138
|
|
|
}); |
139
|
|
|
} |
140
|
|
|
}, |
141
|
|
|
|
142
|
|
|
select: { |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Init the control. |
146
|
|
|
* |
147
|
|
|
* @since 3.0.17 |
148
|
|
|
* @param {Object} control - The control object. |
149
|
|
|
* @param {Object} control.id - The setting. |
150
|
|
|
* @returns {null} |
151
|
|
|
*/ |
152
|
|
|
init: function( control ) { |
153
|
|
|
var element = jQuery( 'select[data-id="' + control.id + '"]' ), |
|
|
|
|
154
|
|
|
multiple = parseInt( element.data( 'multiple' ), 10 ), |
155
|
|
|
selectValue, |
156
|
|
|
selectWooOptions = { |
157
|
|
|
escapeMarkup: function( markup ) { |
158
|
|
|
return markup; |
159
|
|
|
} |
160
|
|
|
}; |
161
|
|
|
|
162
|
|
|
if ( 1 < multiple ) { |
163
|
|
|
selectWooOptions.maximumSelectionLength = multiple; |
164
|
|
|
} |
165
|
|
|
jQuery( element ).selectWoo( selectWooOptions ).on( 'change', function() { |
166
|
|
|
selectValue = jQuery( this ).val(); |
167
|
|
|
selectValue = ( null === selectValue && 1 < multiple ) ? [] : selectValue; |
168
|
|
|
kirki.setting.set( control.id, selectValue ); |
169
|
|
|
}); |
170
|
|
|
} |
171
|
|
|
}, |
172
|
|
|
|
173
|
|
|
image: { |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Get the HTML for image inputs. |
177
|
|
|
* |
178
|
|
|
* @since 3.0.17 |
179
|
|
|
* @param {Object} data - The arguments. |
180
|
|
|
* @returns {string} |
181
|
|
|
*/ |
182
|
|
|
getTemplate: function( data ) { |
183
|
|
|
var html = '', |
|
|
|
|
184
|
|
|
saveAs = 'url', |
|
|
|
|
185
|
|
|
url; |
186
|
|
|
|
187
|
|
|
data = _.defaults( data, { |
|
|
|
|
188
|
|
|
label: '', |
189
|
|
|
description: '', |
190
|
|
|
inputAttrs: '', |
191
|
|
|
'data-id': '', |
192
|
|
|
choices: {}, |
193
|
|
|
value: '' |
194
|
|
|
} ); |
195
|
|
|
|
196
|
|
|
if ( ! _.isUndefined( data.choices ) && ! _.isUndefined( data.choices.save_as ) ) { |
197
|
|
|
saveAs = data.choices.save_as; |
198
|
|
|
} |
199
|
|
|
url = data.value; |
200
|
|
|
if ( _.isObject( data.value ) && ! _.isUndefined( data.value.url ) ) { |
201
|
|
|
url = data.value.url; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
html += '<label>'; |
205
|
|
|
if ( data.label ) { |
206
|
|
|
html += '<span class="customize-control-title">' + data.label + '</span>'; |
207
|
|
|
} |
208
|
|
|
if ( data.description ) { |
209
|
|
|
html += '<span class="description customize-control-description">' + data.description + '</span>'; |
210
|
|
|
} |
211
|
|
|
html += '</label>'; |
212
|
|
|
html += '<div class="image-wrapper attachment-media-view image-upload">'; |
213
|
|
|
if ( data.value.url || '' !== url ) { |
214
|
|
|
html += '<div class="thumbnail thumbnail-image"><img src="' + url + '" alt="" /></div>'; |
215
|
|
|
} else { |
216
|
|
|
html += '<div class="placeholder">' + kirkiL10n.noFileSelected + '</div>'; |
217
|
|
|
} |
218
|
|
|
html += '<div class="actions">'; |
219
|
|
|
html += '<button class="button image-upload-remove-button' + ( '' === url ? ' hidden' : '' ) + '">' + kirkiL10n.remove + '</button>'; |
220
|
|
|
if ( data['default'] && '' !== data['default'] ) { |
221
|
|
|
html += '<button type="button" class="button image-default-button"'; |
222
|
|
|
if ( data['default'] === data.value || ( ! _.isUndefined( data.value.url ) && data['default'] === data.value.url ) ) { |
223
|
|
|
html += ' style="display:none;"'; |
224
|
|
|
} |
225
|
|
|
html += '>' + kirkiL10n['default'] + '</button>'; |
226
|
|
|
} |
227
|
|
|
html += '<button type="button" class="button image-upload-button">' + kirkiL10n.selectFile + '</button>'; |
228
|
|
|
html += '</div></div>'; |
229
|
|
|
|
230
|
|
|
return '<div class="kirki-input-container" data-id="' + data.id + '">' + html + '</div>'; |
231
|
|
|
}, |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Init the control. |
235
|
|
|
* |
236
|
|
|
* @since 3.0.17 |
237
|
|
|
* @param {Object} control - The control object. |
238
|
|
|
* @returns {null} |
239
|
|
|
*/ |
240
|
|
|
init: function( control ) { // jshint ignore:line |
|
|
|
|
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
} ); |
245
|
|
|
|
Since ECMAScript 6, you can create block-scoped vars or constants with the keywords
let
orconst
. These variables/constants are only valid in the code block where they have been declared.Consider the following two pieces of code:
and
The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.
To know more about this ECMA6 feature, look at the MDN pages on let and const.